package com.aba.cjms.admin;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import com.aba.cjms.pojo.ConnectionDatabaseForMySQLUseJdbc;

public class  AdminDao {
	
	   static  Connection connection;
	   static PreparedStatement pstmt;
	   static ResultSet rset;


	   //重置教师的密码
	  public static void resetTeacherPassword() throws Exception
	   {
		   Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();

		   pstmt = connection.prepareStatement("update teacher set tpassword = ? where id = ?");
		   Scanner  sc = new Scanner(System.in);
		   System.out.println("请输入要重置的教师的编号");
		   int id = sc.nextInt();
		   pstmt.setInt(2, id);
		   System.out.println("请输入新密码：");
		   String newPassword = sc.next();
		   pstmt.setString(1, newPassword);
		   if (pstmt.executeUpdate() > 0)
		   {
			   System.out.println("重置密码成功");
		   }
		   else
		   {
			   System.out.println("重置密码失败");
		   }
		   pstmt.close();
		   connection.close();
		   main();
		   return;
	   }
	   //重置学生的密码
	   public static void resetStudentPassword() throws Exception
	   {
		   Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();

		   pstmt = connection.prepareStatement("update student set spassword = ? where id = ?");
		   Scanner  sc = new Scanner(System.in);
		   
		   System.out.println("请输入学生的id号");
		   int id = sc.nextInt();
		   pstmt.setInt(2, id);
		   
		   System.out.println("	请输入重置的密码");
		   String spassword = sc.next();
		   pstmt.setString(1, spassword);
		   
		   
		   if (pstmt.executeUpdate() > 0)
		   {
			   System.out.println("重置密码成功");
			   System.out.println();
		   }
		   else
		   {
			   System.out.println("重置密码失败");
			   System.out.println();
		   }
		   pstmt.close();
		   connection.close();
		   main();
		  return;
	   }



	//批量注册教师账号
	public static void InsertDataTeacher() throws Exception {

		Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
		PreparedStatement pstmt = connection.prepareStatement("insert into teacher(taccount,tname,tpassword) values(?,?,?)");
		while (true) {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入教师账号：");
			String taccount = sc.next();

			System.out.println("请输入教师密码：");
			String tpassword = sc.next();

			System.out.println("请输入教师姓名：");
			String tname = sc.next();

			pstmt.setString(1,taccount);
			pstmt.setString(2, tname);
			pstmt.setString(3, tpassword);
			pstmt.executeUpdate();
			System.out.println("添加教师成功！");


			System.out.println("是否继续添加教师？1.是 2.否");//数值

			int choice = sc.nextInt();

			while (choice != 1 && choice != 2) {
				System.out.println("输入错误，请重新输入！");
				System.out.println("是否继续添加教师？1.是 2.否");
				choice = sc.nextInt();

				
				if (choice == 1) {
					continue;
				} else {
					break;
				}
				
			}
			
			if(choice == 1) {
				continue;
			}
			else {
				System.out.println("退出成功！");
				System.out.println();
				main();
				break;
			}
		}
	}

	//批量注册学生账号
			public static void InsertDataStudent() throws Exception {

		Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
		PreparedStatement pstmt = connection.prepareStatement("insert into student(saccount,sname,spassword) values(?,?,?)");
		while(true)
		{
			Scanner  sc = new Scanner(System.in);
			System.out.println("请输入学生账号：");
			String saccount = sc.next();

			System.out.println("请输入学生密码：");
			String spassword = sc.next();

			System.out.println("请输入学生姓名：");
			String sname = sc.next();

			pstmt.setString(1, saccount);
			pstmt.setString(2, sname);
			pstmt.setString(3, spassword);
			pstmt.executeUpdate();
			System.out.println("添加学生成功！");


			System.out.println("是否继续添加学生？1.是 2.否");//数值

			int choice = sc.nextInt();
			
			while(choice != 1 && choice != 2)
			{ 
				
			
				System.out.println("输入错误，请重新输入！");
				System.out.println("是否继续添加学生？1.是 2.否");
				choice = sc.nextInt();

				
				if(choice == 1)
				{
					continue;
				}
				else
				{
					break;
				}
			}
			if(choice == 1)
			{
				continue;
			}
			else {
				System.out.println("退出成功！");
				System.out.println();
				main();
				break;
			}
		}
	}

			//查寻所有的教师
			public static void SelectAllTeacher() throws Exception {
				  Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
				  pstmt = connection.prepareStatement("select * from teacher");
				  rset = pstmt.executeQuery();
				  while (rset.next()) {
					  System.out.println("教师账号：" + rset.getString("taccount") + "教师姓名：" + rset.getString("tname") + "教师密码：" + rset.getString("tpassword"));
				  }
				  pstmt.close();
				  connection.close();
				  main();
				  return;
			}
			//查询所有的学生
			public static void SelectAllStudent() throws Exception {
			  	
				  Connection connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
				  pstmt = connection.prepareStatement("select * from student");
				  rset = pstmt.executeQuery();
				  while (rset.next()) {
					  System.out.println("学生账号：" + rset.getString("sname") + "学生姓名：" + rset.getString("sname") + "学生密码：" + rset.getString("spassword"));
				  }
				  pstmt.close();
				  connection.close();
				  System.out.println();
				  main();
				  return;		
				  
			}
		
			
			
			
			
		public static void main() throws Exception {
		   //选择操作
			
			System.out.println();
			System.out.println("1.注册教师账号输入：1");

			System.out.println("2.注册学生账号输入：2");

			System.out.println("3.重置教师密码输入：3");

			System.out.println("4.重置学生密码输入：4");
			
			System.out.println("5.查询所有教师输入：5");
			
			
			System.out.println("6.查询所有学生输入：6");
			
			System.out.println("7.退出系统输入：7");
			System.out.println("请输入你的选择：");
			
			Scanner sc = new Scanner(System.in);
			int choice = sc.nextInt();
			
			while(choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6 &&choice!=7)
			{
				System.out.println("输入错误，请重新输入！");
				System.out.println("请输入你的选择：");
				choice = sc.nextInt();
			}
			
			switch(choice){
				case 1:
					InsertDataTeacher();
					break;

					case 2: InsertDataStudent();
						break;

					case 3:
						resetTeacherPassword();
						break;

					case 4:
						resetStudentPassword();
						break;

					case 5:System.out.println("查询所有教师");
						SelectAllTeacher();
						break;
					case 6:System.out.println("查询所有学生");
						SelectAllStudent();
						break;
						
						
					case 7:
						System.out.println("退出系统");
						break;
					default:
						System.out.println("输入错误！请重新输入！");
						break;
			}
		}
		
//		public static void main(String[] args) throws Exception {
//			main();
//		}
//				
}
